﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors;
using System.IO;

namespace PRO_DOCS.DocumentManagementSystem.ADMIN.WIN.Configurations.Dashboard
{
    public partial class RealTimeUserAccessLog : PRO_DOCS.BLL.WIN.Templates.DevxPermissionUserControlTemplate
    {
        private IBindingList _source;
        private Timer _timer;

        public RealTimeUserAccessLog()
        {
            InitializeComponent();

            _timer = new Timer { Enabled = true, Interval = (int)intervalSpinEdit.Value * 1000 };
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Start();

            _source = new BindingList<Pair>();
            chartControl1.Series[0].ArgumentDataMember = "DateTimeStr";
            chartControl1.Series[0].ValueDataMembers.AddRange("Count");
            chartControl1.DataSource = _source;
            var diag = chartControl1.Diagram as XYDiagram;
            if (diag != null)
            {
                diag.AxisY.Range.MinValue = 0;
                diag.AxisY.Range.MaxValue = 10;
                diag.AxisY.GridSpacingAuto = false;
                diag.AxisY.GridSpacing = 1;
            }
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            GetListOfOnlineUsers();
        }


        private void reloadButton_Click(object sender, EventArgs e)
        {
            _timer.Enabled = true;
            _timer.Interval = (int)intervalSpinEdit.Value * 1000;
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Start();
        }


        private void GetListOfOnlineUsers()
        {
            try
            {
                var originalRows = PRO_DOCSDatabaseDataContext.proc_GET_REAL_TIME_USERS_ACCESS_LOG().ToList();

                var current = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour,
                                           DateTime.Now.Minute, DateTime.Now.Second);

                var pr = new Pair { DateTime = current };

                var countOfUsers = originalRows.Count(m => m.loginDateWithoutMils != null &&
                                                           (current >= m.loginDateWithoutMils &&
                                                           (m.logoutDateWithoutMils == null || current <= m.logoutDateWithoutMils)));

                pr.Count = countOfUsers;
                if (_source.Count > 15)
                    _source.RemoveAt(0);
                _source.Add(pr);

                if (pr.Count > 0 && pr.Count > int.Parse(((XYDiagram)chartControl1.Diagram).AxisY.Range.MaxValue.ToString()))
                    ((XYDiagram)chartControl1.Diagram).AxisY.Range.MaxValue = pr.Count + 10;

            }
            catch (Exception ex)
            {
                //return null;
            }
        }

        private void pauseButton_Click(object sender, EventArgs e)
        {
            _timer.Enabled = !_timer.Enabled;
            pauseButton.Text = _timer.Enabled ? "Pause" : "Start";
        }

        #region EXPORT CHART

        private void tsbExportImg_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.AddExtension = true;
            sfd.Filter = "Jpeg file | *.jpeg";
            sfd.Title = "Select Saving Directory";
            if (sfd.ShowDialog().Equals(DialogResult.OK))
            {
                chartControl1.ExportToImage(sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            }

        }

        private void tsbExportPDF_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.AddExtension = true;
            sfd.Filter = "Pdf file | *.pdf";
            sfd.Title = "Select Saving Directory";
            if (sfd.ShowDialog().Equals(DialogResult.OK))
            {
                DevExpress.XtraPrinting.PdfExportOptions PdfExportOptions = new DevExpress.XtraPrinting.PdfExportOptions();
                PdfExportOptions.Compressed = false;
                PdfExportOptions.ConvertImagesToJpeg = true;
                PdfExportOptions.DocumentOptions.Author = "ECHO";
                PdfExportOptions.DocumentOptions.Subject = "Real time user access log report";
                PdfExportOptions.ImageQuality = DevExpress.XtraPrinting.PdfJpegImageQuality.Highest;
                chartControl1.ExportToPdf(sfd.FileName, PdfExportOptions);
            }
        }

        private void tsbExportXLSX_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.AddExtension = true;
            sfd.Filter = "Excel file | *.xlsx";
            sfd.Title = "Select Saving Directory";
            if (sfd.ShowDialog().Equals(DialogResult.OK))
            {
                DevExpress.XtraPrinting.XlsxExportOptions xlsxExportOptions = new DevExpress.XtraPrinting.XlsxExportOptions();
                xlsxExportOptions.ExportMode = DevExpress.XtraPrinting.XlsxExportMode.SingleFile;
                xlsxExportOptions.SheetName = "RTUAL " + DateTime.Now.ToString("dd.MM.yyyy");
                xlsxExportOptions.ExportHyperlinks = true;
                xlsxExportOptions.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text;
                chartControl1.ExportToXlsx(sfd.FileName, xlsxExportOptions);
            }
        }

        private void tsbExportRTF_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.AddExtension = true;
            sfd.Filter = "RFT file | *.rtf";
            sfd.Title = "Select Saving Directory";
            if (sfd.ShowDialog().Equals(DialogResult.OK))
            {
                chartControl1.ExportToRtf(sfd.FileName);
            }
        } 
        #endregion

    }

    public class Pair
    {
        public DateTime DateTime { get; set; }

        public int Count { get; set; }

        public string DateTimeStr
        {
            get { return DateTime.ToString("dd-MM-yyyy hh:mm:ss tt"); }
        }
    }
}
